package org.keyroy.map.core;

import java.util.List;
import java.util.Properties;

import org.keyroy.map.core.overlays.ObjOverlay;
import org.keyroy.map.core.overlays.TilesOverlay;
import org.keyroy.map.core.sprite.Sprite;
import org.keyroy.util.tag.Tag;
import org.keyroy.util.tag.XmlSupport;

public class Map implements XmlSupport {
	public static final String TAG_FLOOR = "floor";
	public static final String TAG_BULIDING = "buliding";

	public String name;
	public int columns, rows;
	public int gridWidth, gridHeight;

	public Properties properties;

	public TilesOverlay floors;
	public TilesOverlay buildings;
	public ObjOverlay objOverlay = new ObjOverlay();

	public Map() {
	}

	public Map(String name, int columns, int rows, int gridWidth, int gridHeight) {
		this();
		set(name, columns, rows, gridWidth, gridHeight);
	}

	public void set(String name, int columns, int rows, int gridWidth, int gridHeight) {
		this.name = name;
		this.columns = columns;
		this.rows = rows;
		this.gridWidth = gridWidth;
		this.gridHeight = gridHeight;
		this.floors = new TilesOverlay(this);
		this.floors.name = TAG_FLOOR;
		this.buildings = new TilesOverlay(this);
		this.buildings.name = TAG_BULIDING;
	}

	public void setName(String name) {
		this.name = name;
	}

	public final String getName() {
		return name;
	}

	public final int getColumns() {
		return columns;
	}

	public final int getRows() {
		return rows;
	}

	public final int getGridWidth() {
		return gridWidth;
	}

	public final int getGridHeight() {
		return gridHeight;
	}

	public final TilesOverlay getFloors() {
		return floors;
	}

	public final Sprite getTiles(int x, int y) {
		Sprite sprite = null;
		try {
			if (buildings != null) {
				sprite = buildings.get(x, y);
			}

			if (sprite == null) {
				sprite = floors.get(x, y);
			}
		} catch (Exception e) {
		}
		return sprite;
	}

	@Override
	public void initTag(Tag tag) {
		tag.addParameter("name", name);
		tag.addParameter("columns", columns);
		tag.addParameter("rows", rows);
		tag.addParameter("gridWidth", gridWidth);
		tag.addParameter("gridHeight", gridHeight);

		if (floors != null && floors.isDirty()) {
			tag.addChild(floors);
		}

		if (buildings != null && buildings.isDirty()) {
			tag.addChild(buildings);
		}

		if (objOverlay != null && objOverlay.isDirty()) {
			tag.addChild(objOverlay);
		}
	}

	@Override
	public void initObj(Tag tag) {
		this.name = tag.getParameterValue("name");
		this.columns = tag.getParameterIntValue("columns");
		this.rows = tag.getParameterIntValue("rows");
		this.gridWidth = tag.getParameterIntValue("gridWidth");
		this.gridHeight = tag.getParameterIntValue("gridHeight");
		set(name, columns, rows, gridWidth, gridHeight);

		List<Tag> tags = tag.getChildTagArray(floors.getTagName());
		for (int i = 0; i < tags.size(); i++) {
			Tag cTag = tags.get(i);
			if (cTag.getParameterValue("name").equals(TAG_FLOOR)) {
				floors.initObj(cTag);
			} else if (cTag.getParameterValue("name").equals(TAG_BULIDING)) {
				buildings.initObj(cTag);
			}
		}

		ObjOverlay objOverlay = (ObjOverlay) tag.getChildObject(ObjOverlay.class);
		if (objOverlay != null) {
			this.objOverlay = objOverlay;
		}
	}

	@Override
	public String getTagName() {
		return getClass().getSimpleName();
	}
}
